﻿namespace Bt.Mvc
{
    using System;
    using System.Text;
    using Microsoft.SPOT;
    using Microsoft.SPOT.Hardware;
    using System.Threading;
    using System.IO;
    using Bt.WebServer;

    public class Controller
    {
        public ViewResult ExecuteRequest(HttpRequest request)
        {
            var router = new Router(request.Url);
            string actionName = router.Action;
            if (actionName != null) 
            {
                var actionMethods = this.GetType().GetMethods();
                foreach(var action in actionMethods)
                {
                    if (string.Compare(action.Name.ToLower(), actionName.ToLower())==0) 
                    {
                        return (ViewResult)action.Invoke(this, null);
                    }
                }
            }

            return ViewResult.NotFound;
        }

        // actionName = this.GetActionName(request);

        //[Obsolete]
        //private string GetActionName(HttpRequest request)
        //{
        //    if (request.Url.IndexOf("Open") > 0) 
        //    {
        //        return "open";
        //    } else { 
        //        return "index"; 
        //    }
        //    // http://www.seznam.cz/door/index?para=value
        //}


        /// <summary>
        ///  Rozpracované 
        ///  potřeba dodělat načítání z SD
        /// </summary>
        /// <param name="viewName"></param>
        /// <returns></returns>
        private FillableViewResult View(string viewName) 
        {
            var controllerName = this.GetType().FullName;
            var path =  Path.Combine(controllerName, viewName);

            return new FillableViewResult() { Body = path };
        }
    }
}
